package cz.data.domain.quality.controller;

import cz.data.common.base.SimpleDataResponse;
import cz.data.common.base.TableDataResponse;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cz.data.common.base.BaseController;
import cz.data.common.core.JsonPage;
import cz.data.domain.quality.mapstruct.CheckReportMapper;
import cz.data.domain.quality.model.entity.CheckReportEntity;
import cz.data.domain.quality.model.entity.DataReportEntity;
import cz.data.domain.quality.model.query.CheckReportQuery;
import cz.data.domain.quality.model.vo.CheckReportVo;
import cz.data.domain.quality.service.CheckReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Api(tags = {"核查报告"})
@RestController
@RequestMapping("/checkReports")
public class CheckReportController extends BaseController {

    @Autowired
    private CheckReportService checkReportService;

    @Autowired
    private CheckReportMapper checkReportMapper;

    @ApiOperation(value = "核查报告-详情", notes = "根据url的id来获取详细信息")
    @ApiImplicitParam(name = "id", value = "ID", required = true, dataType = "String", paramType = "path")
    @GetMapping("/{id}")
    public SimpleDataResponse<CheckReportVo> getCheckReportById(@PathVariable String id) {
        CheckReportEntity checkReportEntity = checkReportService.getCheckReportById(id);
        return new SimpleDataResponse<>(checkReportMapper.toVO(checkReportEntity));
    }

    @ApiOperation(value = "核查报告-分页查询")
    @ApiImplicitParam(name = "checkReportQuery", value = "查询实体checkReportQuery", required = true, dataTypeClass = CheckReportQuery.class)
    @GetMapping("/page")
    public SimpleDataResponse<JsonPage<CheckReportVo>> getCheckReportPage(CheckReportQuery checkReportQuery) {
        QueryWrapper<CheckReportEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(checkReportQuery.getRuleTypeId()), "r.rule_type_id", checkReportQuery.getRuleTypeId());
        queryWrapper.like(StringUtils.isNotBlank(checkReportQuery.getRuleName()), "r.rule_name", checkReportQuery.getRuleName());
        queryWrapper.like(StringUtils.isNotBlank(checkReportQuery.getRuleSource()), "r.rule_source", checkReportQuery.getRuleSource());
        queryWrapper.like(StringUtils.isNotBlank(checkReportQuery.getRuleTable()), "r.rule_table", checkReportQuery.getRuleTable());
        queryWrapper.like(StringUtils.isNotBlank(checkReportQuery.getRuleColumn()), "r.rule_column", checkReportQuery.getRuleColumn());
        // 确定唯一核查报告
        queryWrapper.apply("c.check_batch = r.last_check_batch");
        IPage<CheckReportEntity> page = checkReportService.page(new Page<>(checkReportQuery.getPageNum(), checkReportQuery.getPageSize()), queryWrapper);
        List<CheckReportVo> collect = page.getRecords().stream().map(checkReportMapper::toVO).collect(Collectors.toList());
        JsonPage<CheckReportVo> jsonPage = new JsonPage<>(page.getCurrent(), page.getSize(), page.getTotal(), collect);
        return new SimpleDataResponse<>(jsonPage);
    }

    @GetMapping("/getReportBySource")
    public TableDataResponse<DataReportEntity> getReportBySource(CheckReportQuery checkReportQuery) {
        LocalDate checkDate = checkReportQuery.getCheckDate();
        String date = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(checkDate);
        List<DataReportEntity> list = checkReportService.getReportBySource(date);
        return new TableDataResponse<>(list);
    }

    @GetMapping("/getReportByType")
    public TableDataResponse<DataReportEntity> getReportByType(CheckReportQuery checkReportQuery) {
        LocalDate checkDate = checkReportQuery.getCheckDate();
        String date = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(checkDate);
        List<DataReportEntity> list = checkReportService.getReportByType(date);
        return new TableDataResponse<>(list);
    }

    @GetMapping("/getReportDetail")
    public SimpleDataResponse<Map<String, Object>> getReportDetail(CheckReportQuery checkReportQuery) {
        LocalDate checkDate = checkReportQuery.getCheckDate();
        String date = DateTimeFormatter.ofPattern("yyyy-MM-dd").format(checkDate);
        Map<String, Object> map = checkReportService.getReportDetail(date);
        return new SimpleDataResponse<>(map);
    }
}
